package com.example.composewebview.Utils.PermissionUtil
import android.Manifest
import android.os.Build
import androidx.activity.result.ActivityResultLauncher

fun reqPermission(
    capability: String,
    launcher: ActivityResultLauncher<Any>,
    completion: (Boolean) -> Unit
) {
    val isAuthorized = when (capability) {
        "camera" -> {
            requestCameraPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "contacts", "contacts-r" -> {
            requestContactsPermission(launcher as ActivityResultLauncher<Array<String>>)
            true
        }
        "contacts-w" -> {
            requestContactsWritePermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "microphone" -> {
            requestMicrophonePermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "photos" -> {
            requestPhotosPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "photos-w" -> {
            requestPhotosWritePermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "location" -> {
            requestLocationPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "notification" -> {
            requestNotificationPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "calendar" -> {
            requestCalendarPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "calendar-r" -> {
            requestCalendarReadPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "calendar-w" -> {
            requestCalendarWritePermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "phone" -> {
            requestPhonePermission(launcher as ActivityResultLauncher<Array<String>>)
            true
        }
        "phone-call" -> {
            requestPhoneCallPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "phone-read" -> {
            requestPhoneReadPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "phone-read-log" -> {
            requestPhoneReadLogPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "phone-write-log" -> {
            requestPhoneWriteLogPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "sensor" -> {
            requestSensorPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "sms" -> {
            requestSMSPermission(launcher as ActivityResultLauncher<Array<String>>)
            true
        }
        "sms-read" -> {
            requestSMSReadPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "sms-send" -> {
            requestSMSSendPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "storage" -> {
            requestStoragePermission(launcher as ActivityResultLauncher<Array<String>>)
            true
        }
        "storage-read" -> {
            requestStorageReadPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "storage-write" -> {
            requestStorageWritePermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "ble-scan" -> {
            requestBleScanPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "ble-ad" -> {
            requestBleAdPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        "ble-connect" -> {
            requestBleConnectPermission(launcher as ActivityResultLauncher<String>)
            true
        }
        else -> {
            false
        }
    }

    completion(isAuthorized);


}

// 相机/拍照/录像权限
fun requestCameraPermission(launcher: ActivityResultLauncher<String>) {
    val cameraPermission = Manifest.permission.CAMERA
    launcher.launch(cameraPermission)
}

// 联系人读取/写入权限
fun requestContactsPermission(launcher: ActivityResultLauncher<Array<String>>) {
    val readPermission = Manifest.permission.READ_CONTACTS
    val writePermission = Manifest.permission.WRITE_CONTACTS
    val permissions = arrayOf(readPermission, writePermission)
    launcher.launch(permissions)
}

// 联系人仅读
fun requestContactsReadPermission(launcher: ActivityResultLauncher<String>) {
    val readPermission = Manifest.permission.READ_CONTACTS
    launcher.launch(readPermission)
}

// 联系人仅写
fun requestContactsWritePermission(launcher: ActivityResultLauncher<String>) {
    val writePermission = Manifest.permission.WRITE_CONTACTS
    launcher.launch(writePermission)
}

// 使用麦克风录制音频权限
fun requestMicrophonePermission(launcher: ActivityResultLauncher<String>) {
    val microphonePermission = Manifest.permission.RECORD_AUDIO
    launcher.launch(microphonePermission)
}

//访问相册|本地存储空间。Android上等同于storage
fun requestPhotosPermission(launcher: ActivityResultLauncher<String>) {
    val photosPermission = Manifest.permission.READ_EXTERNAL_STORAGE
    launcher.launch(photosPermission)
}

//仅写入相册|本地存储空间
fun requestPhotosWritePermission(launcher: ActivityResultLauncher<String>) {
    val photosWritePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE
    launcher.launch(photosWritePermission)
}

//定位
fun requestLocationPermission(launcher: ActivityResultLauncher<String>) {
    val locationPermission = Manifest.permission.ACCESS_FINE_LOCATION
    launcher.launch(locationPermission)
}

//状态栏通知
fun requestNotificationPermission(launcher: ActivityResultLauncher<String>) {
    val notificationPermission = Manifest.permission.ACCESS_NOTIFICATION_POLICY
    launcher.launch(notificationPermission)
}

//日历读取/写入。只支持Android
fun requestCalendarPermission(launcher: ActivityResultLauncher<String>) {
    val calendarPermission = Manifest.permission.READ_CALENDAR
    launcher.launch(calendarPermission)
}

//仅日历读取
fun requestCalendarReadPermission(launcher: ActivityResultLauncher<String>) {
    val calendarReadPermission = Manifest.permission.READ_CALENDAR
    launcher.launch(calendarReadPermission)
}

//仅日历写入
fun requestCalendarWritePermission(launcher: ActivityResultLauncher<String>) {
    val calendarWritePermission = Manifest.permission.WRITE_CALENDAR
    launcher.launch(calendarWritePermission)
}

//直接拨打电话/获取手机号码|IMEI。只支持Android
fun requestPhonePermission(launcher: ActivityResultLauncher<Array<String>>) {
    val phonePermission = arrayOf(
        Manifest.permission.CALL_PHONE,
        Manifest.permission.READ_PHONE_STATE
    )
    launcher.launch(phonePermission)
}

//仅直接拨打电话
fun requestPhoneCallPermission(launcher: ActivityResultLauncher<String>) {
    val phoneCallPermission = Manifest.permission.CALL_PHONE
    launcher.launch(phoneCallPermission)
}

//仅获取手机号码|IMEI
fun requestPhoneReadPermission(launcher: ActivityResultLauncher<String>) {
    val phoneReadPermission = Manifest.permission.READ_PHONE_STATE
    launcher.launch(phoneReadPermission)
}

//读取通话记录
fun requestPhoneReadLogPermission(launcher: ActivityResultLauncher<String>) {
    val phoneReadLogPermission = Manifest.permission.READ_CALL_LOG
    launcher.launch(phoneReadLogPermission)
}

//写入通话记录
fun requestPhoneWriteLogPermission(launcher: ActivityResultLauncher<String>) {
    val phoneWriteLogPermission = Manifest.permission.WRITE_CALL_LOG
    launcher.launch(phoneWriteLogPermission)
}

//传感器.只支持Android
fun requestSensorPermission(launcher: ActivityResultLauncher<String>) {
    val sensorPermission = Manifest.permission.BODY_SENSORS
    launcher.launch(sensorPermission)
}

//读取短信/后台发送短信。只支持Android
fun requestSMSPermission(launcher: ActivityResultLauncher<Array<String>>) {
    val smsPermission = arrayOf(
        Manifest.permission.READ_SMS,
        Manifest.permission.SEND_SMS
    )
    launcher.launch(smsPermission)
}

//仅后台发送短信
fun requestSMSReadPermission(launcher: ActivityResultLauncher<String>) {
    val smsReadPermission = Manifest.permission.READ_SMS
    launcher.launch(smsReadPermission)
}

//仅读取短信
fun requestSMSSendPermission(launcher: ActivityResultLauncher<String>) {
    val smsSendPermission = Manifest.permission.SEND_SMS
    launcher.launch(smsSendPermission)
}

//读取/写入|相册|多媒体|本地存储空间。只支持Android
fun requestStoragePermission(launcher: ActivityResultLauncher<Array<String>>) {
    val storagePermission = arrayOf(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )
    launcher.launch(storagePermission)
}

//仅读取|相册|多媒体|文件|本地存储空间
fun requestStorageReadPermission(launcher: ActivityResultLauncher<String>) {
    val storageReadPermission = Manifest.permission.READ_EXTERNAL_STORAGE
    launcher.launch(storageReadPermission)
}

//仅写入|相册|多媒体|文件|本地存储空间
fun requestStorageWritePermission(launcher: ActivityResultLauncher<String>) {
    val storageWritePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE
    launcher.launch(storageWritePermission)
}

//发现和配对附近的蓝牙设备。Android12及以上有效，低版本默认为true
fun requestBleScanPermission(launcher: ActivityResultLauncher<String>) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val bleScanPermission = Manifest.permission.BLUETOOTH_SCAN
        launcher.launch(bleScanPermission)
    } else {
        // 在低于 Android 12 的版本上，蓝牙扫描权限默认为已授予
        // 执行相应的操作
    }
}

//向附近的蓝牙设备发布广告。Android12及以上有效，低版本默认为true
fun requestBleAdPermission(launcher: ActivityResultLauncher<String>) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val bleAdPermission = Manifest.permission.BLUETOOTH_ADVERTISE
        launcher.launch(bleAdPermission)
    } else {
        // 在低于 Android 12 的版本上，蓝牙广告发布权限默认为已授予
        // 执行相应的操作
    }
}

//连接到配对的蓝牙设备。Android12及以上有效，低版本默认为true
fun requestBleConnectPermission(launcher: ActivityResultLauncher<String>) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        val bleConnectPermission = Manifest.permission.BLUETOOTH_CONNECT
        launcher.launch(bleConnectPermission)
    } else {
        // 在低于 Android 12 的版本上，蓝牙连接权限默认为已授予
        // 执行相应的操作
    }
}
